
package xj.graph2d.app.diagram;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;

import xj.graph2d.app.DrawAction;

public class BuildStateDiagramTestListener 
  extends DrawAction 
  implements ActionListener {

  StateDiagramBuilder builder;

  public void actionPerformed(ActionEvent e) {
    if (e != null) { 
      Object src =e.getSource();
      if (src instanceof JMenuItem) { 
	JMenuItem mi = (JMenuItem) src;
	String cmd = mi.getActionCommand(); 

	char c = cmd.charAt(5);
	builder = new StateDiagramBuilder(main, "State Diagram Test " + c, (c != '1'));

	if ("State1".equals(cmd)) { 
	  test1();
	} else if ("State2".equals(cmd)) { 
	  test2(); 
	} else if ("State3".equals(cmd)) { 
	  test3(); 
	} else if ("State4".equals(cmd)) { 
	  test4(); 
	} else if ("State5".equals(cmd)) { 
	  test5(); 
	} else if ("State6".equals(cmd)) { 
	  test6(); 
	} else if ("State7".equals(cmd)) { 
	  test7(); 
	} else if ("State8".equals(cmd)) { 
	  test8(); 
	} else if ("State9".equals(cmd)) { 
	  test9(); 
	}

	/*
	Thread t = new Thread(new StateDiagramTestRunner(cmd));
	t.start();
	*/
      }
    }
  }

  class StateDiagramTestRunner implements Runnable {

    String cmd; 

    StateDiagramTestRunner(String cmd) { 
      this.cmd = cmd; 
    }

    public void run() { 
      if ("State1".equals(cmd)) { 
	test1();
      } else if ("State2".equals(cmd)) { 
	test2(); 
      } else if ("State3".equals(cmd)) { 
	test3(); 
      } else if ("State4".equals(cmd)) { 
	test4(); 
      } else if ("State5".equals(cmd)) { 
	test5(); 
      } else if ("State6".equals(cmd)) { 
	test6(); 
      } else if ("State7".equals(cmd)) { 
	test7(); 
      } else if ("State8".equals(cmd)) { 
	test8(); 
      } else if ("State9".equals(cmd)) { 
	//main.hourGlassOn();
	test9(); 
	//main.hourGlassOff();
      }
    }
  }

  private void test1() { 
    builder.addInitialState("s0");
    builder.addFinalState("s10");
    
    addStates1();
    addStates2();
    addStates3();
    addStates4();
    addStates5();
    addStates6();
    addStates7();
    addStates8();
    addStates9();

    builder.doLayout();
  }

  private void addStates1() { 
    builder.addSimpleState("s1");
    builder.addSimpleState("s2");
    builder.addSimpleState("s3");
    builder.addSimpleState("s4444444444");

    builder.addTransition("s0", "s1");
    builder.addTransition("s1", "s2");
    builder.addTransition("s1", "s3");
    builder.addTransition("s2", "s4444444444");
    builder.addTransition("s3", "s4444444444");
    builder.addTransition("s4444444444", "s10");

    builder.addTransition("s2", "s1"); // double edge
    builder.addTransition("s3", "s1"); // double edge
    builder.addTransition("s4444444444", "s1"); // back edge
  }

  private void addStates2() { 
    builder.addCompoundState("s6");
    builder.addInitialState("s60", "s6");
    builder.addSimpleState("s61", "s6");
    builder.addSimpleState("s62", "s6");

    builder.addTransition("s6.s60", "s6.s61");
    builder.addTransition("s6.s61", "s6.s62");

    builder.addTransition("s1", "s6");
    builder.addTransition("s3", "s6.s61");    

    //builder.addTransition("s2", "s6.s61");    
  }

  private void addStates3() { 
    builder.addCompoundState("s5");
    builder.addInitialState("s50", "s5");
    builder.addSimpleState("s51", "s5");
    builder.addSimpleState("s52", "s5");

    builder.addTransition("s5.s50", "s5.s51");
    builder.addTransition("s5.s51", "s5.s52", "e1", "x > 1", "doX()");
  }

  private void addStates4() { 
    builder.addCompoundState("s7");
    builder.addInitialState("s70", "s7");
    builder.addSimpleState("s71", "s7");
    builder.addSimpleState("s72", "s7");

    builder.addTransition("s7.s70", "s7.s71");
    builder.addTransition("s7.s71", "s7.s72");
  }

  private void addStates5() { 
    builder.addCompoundState("s8");
    builder.addInitialState("s80", "s8");
    builder.addSimpleState("s81", "s8");
    builder.addSimpleState("s82", "s8");

    builder.addTransition("s8.s80", "s8.s81");
    builder.addTransition("s8.s81", "s8.s82");
  }

  private void addStates6() { 
    builder.addCompoundState("s9");
    builder.addInitialState("s90", "s9");
    builder.addSimpleState("s91", "s9");
    builder.addSimpleState("s92", "s9");

    builder.addTransition("s9.s90", "s9.s91");
    builder.addTransition("s9.s91", "s9.s92");
  }

  private void addStates7() { 
    builder.addCompoundState("s100");
    builder.addInitialState("s0", "s100");
    builder.addSimpleState("s1", "s100");
    builder.addSimpleState("s2", "s100");
    builder.addSimpleState("s3", "s100");
    builder.addSimpleState("s4", "s100");
    builder.addFinalState("s10", "s100");
    builder.addCompoundState("s105", "s100");
    builder.addInitialState("s0", "s100.s105");
    builder.addSimpleState("s1", "s100.s105");
    builder.addSimpleState("s2", "s100.s105");
    builder.addSimpleState("s3", "s100.s105");
    builder.addSimpleState("s4", "s100.s105");
    builder.addFinalState("s10", "s100.s105");
  }

  private void addStates8() { 
    builder.addTransition("s100.s0", "s100.s1");
    builder.addTransition("s100.s1", "s100.s2");
    builder.addTransition("s100.s1", "s100.s3");
    builder.addTransition("s100.s2", "s100.s4");
    builder.addTransition("s100.s3", "s100.s4");
    builder.addTransition("s100.s4", "s100.s10");

    builder.addTransition("s100.s105.s0", "s100.s105.s1");
    builder.addTransition("s100.s105.s1", "s100.s105.s2");
    builder.addTransition("s100.s105.s1", "s100.s105.s3");
    builder.addTransition("s100.s105.s2", "s100.s105.s4");
    builder.addTransition("s100.s105.s3", "s100.s105.s4");
    builder.addTransition("s100.s105.s4", "s100.s105.s10");

    builder.addTransition("s100.s1", "s100.s105");
    builder.addTransition("s100.s105", "s100.s4");
  }

  private void addStates9() { 
    //builder.addOrthogonalState("s200", 3);
    //builder.addOrthogonalState("s200", 4);
    builder.addOrthogonalState("s200", 2);
    builder.addInitialState("s00", "s200", 0); 
    builder.addSimpleState("s01", "s200", 0);
    builder.addSimpleState("s02", "s200", 0);
    builder.addSimpleState("s03", "s200", 0);
    builder.addSimpleState("s04", "s200", 0);
    builder.addFinalState("s09", "s200", 0);
    builder.addInitialState("s10", "s200", 1); 
    builder.addSimpleState("s11", "s200", 1);
    builder.addSimpleState("s12", "s200", 1);
    builder.addSimpleState("s13", "s200", 1);
    builder.addFinalState("s19", "s200", 1);
    
    builder.addTransition("s200[0].s00", "s200[0].s01");
    builder.addTransition("s200[0].s01", "s200[0].s02");
    builder.addTransition("s200[0].s01", "s200[0].s03");
    builder.addTransition("s200[0].s02", "s200[0].s09");
    builder.addTransition("s200[0].s03", "s200[0].s09");

    builder.addTransition("s200[1].s10", "s200[1].s11");
    builder.addTransition("s200[1].s11", "s200[1].s12");
    builder.addTransition("s200[1].s11", "s200[1].s13");
    builder.addTransition("s200[1].s12", "s200[1].s19");
    builder.addTransition("s200[1].s13", "s200[1].s19");  
  }

  private void test2() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.2", true);

    builder.addInitialState("s0");
    builder.addFinalState("s10");

    builder.addSimpleState("s1");
    builder.addSimpleState("s2");
    builder.addSimpleState("s3");
    builder.addSimpleState("s4444444444");

    builder.addTransition("s0", "s1");
    builder.addTransition("s1", "s2");
    builder.addTransition("s1", "s3");
    builder.addTransition("s2", "s4444444444");
    builder.addTransition("s3", "s4444444444");
    builder.addTransition("s4444444444", "s10");

    builder.addTransition("s2", "s1"); // double edge
    builder.addTransition("s3", "s1"); // double edge
    builder.addTransition("s4444444444", "s1"); // back edge
    
    builder.addCompoundState("s6");
    builder.addInitialState("s60", "s6");
    builder.addSimpleState("s61", "s6");
    builder.addSimpleState("s62", "s6");

    builder.addTransition("s6.s60", "s6.s61");
    builder.addTransition("s6.s61", "s6.s62");

    builder.addTransition("s1", "s6");
    builder.addTransition("s3", "s6.s61");    

    builder.addCompoundState("s77777777777777777777");

    builder.doLayout();
  }

  private void test3() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.3", true);
    //builder.addInitialState("s0");
    //builder.addFinalState("s10");

    builder.addOrthogonalState("s100", 2);

    builder.addInitialState("s00", "s100", 0); 
    builder.addOrthogonalState("s200", 2, "s100");

    builder.addInitialState("s00", "s100[0].s200", 0); 
    builder.addSimpleState("s01", "s100[0].s200", 0);
    builder.addFinalState("s02", "s100[0].s200", 0);

    builder.addOrthogonalState("s300", 2, "s100[0].s200", 0);
    builder.addInitialState("s00", "s100[0].s200[0].s300", 0); 
    builder.addSimpleState("s01", "s100[0].s200[0].s300", 0);
    builder.addFinalState("s02", "s100[0].s200[0].s300", 0);

    builder.addInitialState("s00", "s100[0].s200", 1); 
    builder.addCompoundState("s301", "s100[0].s200", 1);
    builder.addSimpleState("s302", "s100[0].s200", 1);
    builder.addInitialState("s00", "s100[0].s200[1].s301"); 
    builder.addOrthogonalState("s400", 2, "s100[0].s200[1].s301");

    builder.addInitialState("s00", "s100[0].s200[1].s301.s400", 0); 
    builder.addSimpleState("s01", "s100[0].s200[1].s301.s400", 0);
    builder.addFinalState("s02", "s100[0].s200[1].s301.s400", 0);

    builder.doLayout();
    
    builder.addTransition("s100[0].s200[0].s00", "s100[0].s200[0].s01");
    builder.addTransition("s100[0].s200[0].s01", "s100[0].s200[0].s02");
    builder.addTransition("s100[0].s200[1].s00", "s100[0].s200[1].s301");
    builder.addTransition("s100[0].s200[1].s301", "s100[0].s200[1].s302");

    builder.addTransition("s100[0].s200[0].s300[0].s00", "s100[0].s200[0].s300[0].s01");
    builder.addTransition("s100[0].s200[0].s300[0].s01", "s100[0].s200[0].s300[0].s02");

    builder.addTransition("s100[0].s200[1].s301.s400[0].s00", "s100[0].s200[1].s301.s400[0].s01");
    builder.addTransition("s100[0].s200[1].s301.s400[0].s01", "s100[0].s200[1].s301.s400[0].s02");
    
    builder.doLayout();
  }

  private void test4() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.4", true);
    builder.addInitialState("s0");
    builder.addOrthogonalState("s100", 2);
    builder.addSimpleState("s1");
    builder.addFinalState("s2");

    builder.addInitialState("s00", "s100", 0); 
    builder.addCompoundState("s200", "s100", 0);
    builder.addFinalState("s01", "s100", 0);

    builder.addInitialState("s00", "s100[0].s200"); 
    builder.addSimpleState("s01", "s100[0].s200"); 
    builder.addSimpleState("s02", "s100[0].s200"); 
    builder.addSimpleState("s03", "s100[0].s200"); 
    builder.addFinalState("s04", "s100[0].s200");

    builder.addInitialState("s00", "s100", 1); 
    builder.addOrthogonalState("s201", 2, "s100", 1);
    builder.addSimpleState("s01", "s100", 1); 
    builder.addSimpleState("s02", "s100", 1); 
    builder.addSimpleState("s04", "s100", 1); 
    builder.addFinalState("s03", "s100", 1);

    builder.addInitialState("s00", "s100[1].s201", 0); 
    builder.addSimpleState("s01", "s100[1].s201", 0); 
    builder.addFinalState("s02", "s100[1].s201", 0);

    builder.doLayout();

    builder.addTransition("s0", "s100");
    builder.addTransition("s100", "s1");
    builder.addTransition("s1", "s2");

    builder.addTransition("s100[0].s00", "s100[0].s200");
    builder.addTransition("s100[0].s200", "s100[0].s01");

    builder.addTransition("s100[0].s200.s00", "s100[0].s200.s01");
    builder.addTransition("s100[0].s200.s01", "s100[0].s200.s02");
    builder.addTransition("s100[0].s200.s02", "s100[0].s200.s03");
    builder.addTransition("s100[0].s200.s03", "s100[0].s200.s04");

    builder.addTransition("s100[1].s00", "s100[1].s201");
    builder.addTransition("s100[1].s201", "s100[1].s01");
    builder.addTransition("s100[1].s01", "s100[1].s02");
    builder.addTransition("s100[1].s02", "s100[1].s03");

    builder.addTransition("s100[1].s201[0].s00", "s100[1].s201[0].s01");
    builder.addTransition("s100[1].s201[0].s01", "s100[1].s201[0].s02");

    builder.addTransition("s100[1].s201[0].s01", "s100[1].s01");
    builder.addTransition("s1", "s100[1].s01");

    builder.doLayout();

  }

  private void test5() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.5", true);
    builder.addInitialState("s0");
    builder.addSimpleState("s1");
    builder.addCompoundState("s2");
    builder.addFinalState("s3");

    builder.addSimpleState("s20", "s2");
    builder.addFinalState("s21", "s2");

    builder.addOrthogonalState("s100", 2);

    builder.addInitialState("s00", "s100", 0); 
    builder.addSimpleState("s01", "s100", 0); 
    builder.addFinalState("s02", "s100", 0);
    builder.addSimpleState("s03", "s100", 1); 

    builder.addTransition("s0", "s1");
    builder.addTransition("s1", "s2");
    builder.addTransition("s2", "s3");

    builder.addTransition("s1", "s2.s20");
    builder.addTransition("s2.s20", "s2.s21");

    builder.addTransition("s100[0].s01", "s100[1].s03");

    builder.doLayout();
  }

  private void test6() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.6", true);
    builder.addInitialState("s0");
    builder.addCompoundState("s100");
    builder.addFinalState("s2");

    builder.addInitialState("s00", "s100"); 
    builder.addSimpleState("s01", "s100"); 
    builder.addSimpleState("s02", "s100"); 
    builder.addSimpleState("s03", "s100"); 
    builder.addFinalState("s04", "s100");


    builder.addTransition("s0", "s100");
    builder.addTransition("s100", "s2");

    builder.addTransition("s100.s00", "s100.s01");
    builder.addTransition("s100.s01", "s100.s02");
    builder.addTransition("s100.s01", "s100.s03");
    builder.addTransition("s100.s02", "s100.s03");
    builder.addTransition("s100.s03", "s100.s04");
    builder.doLayout();
  }

  private void test7() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.7", true);
    builder.addInitialState("s0");
    builder.addSimpleState("s1");
    builder.addSimpleState("s2");
    builder.addCompoundState("s100");
    builder.addFinalState("s3");

    builder.addInitialState("s00", "s100"); 
    builder.addSimpleState("s01", "s100"); 
    builder.addFinalState("s02", "s100");

    builder.addTransition("s0", "s1");
    builder.addTransition("s1", "s3");
    builder.addTransition("s1", "s2");
    builder.addTransition("s2", "s1");

    builder.addTransition("s100", "s2");
    builder.addTransition("s2", "s100");

    builder.addTransition("s100.s00", "s100.s01");
    builder.addTransition("s100.s01", "s100.s02");

    builder.addTransition("s1", "s100.s01");
    builder.addTransition("s100.s01", "s2");

    builder.doLayout();
  }

  private void test8() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.8", true);
    builder.addCompoundState("s1");
    builder.addSimpleState("s2");

    builder.addCompoundState("s11", "s1");
    builder.addCompoundState("s12", "s1");
    builder.addSimpleState("s13", "s1");

    builder.addOrthogonalState("s111", 2, "s1.s11");
    builder.addSimpleState("s112", "s1.s11");

    builder.addSimpleState("s121", "s1.s12");

    builder.addSimpleState("s1111", "s1.s11.s111", 0);

    builder.addTransition("s1.s11.s112", "s1.s11.s111[0].s1111");
    builder.addTransition("s1.s11.s112", "s1.s13");

    builder.addTransition("s1.s11.s111[0].s1111", "s1.s12.s121");
    builder.addTransition("s1.s12.s121", "s2");


    builder.doLayout();
  }

  private void test9() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.9");
    builder.addInitialState("s0");
    builder.addSimpleState("s1");
    builder.addSimpleState("s2");

    builder.addTransition("s0", "s1");
    builder.addTransition("s2", "s1");

    try { 
      Thread.currentThread().sleep(10000);
    } catch (InterruptedException e) {}

    builder.doLayout();
  }

  private void test10() { 
    //builder = new StateDiagramBuilder(main, "State Diagram Test 1.10", true);

    builder.doLayout();
  }

}
